using System;
using System.Data;

namespace Sunny.UI;

public static class DataReaderEx
{
	public static bool IsInState(this IDbConnection connection, ConnectionState state)
	{
		if (connection != null)
		{
			return (connection.State & state) == state;
		}
		return false;
	}

	public static void OpenIfNot(this IDbConnection connection)
	{
		if (!connection.IsInState(ConnectionState.Open))
		{
			connection.Open();
		}
	}

	public static T Get<T>(this IDataReader reader, string field)
	{
		return reader.Get(field, default(T));
	}

	public static T Get<T>(this IDataReader reader, string field, T defaultValue)
	{
		object obj = reader[field];
		if (obj == DBNull.Value)
		{
			return defaultValue;
		}
		if (obj is T)
		{
			return (T)obj;
		}
		return defaultValue;
	}

	public static byte[] GetBytes(this IDataReader reader, string field)
	{
		return reader[field] as byte[];
	}

	public static string GetString(this IDataReader reader, string field)
	{
		return reader.GetString(field, null);
	}

	public static string GetString(this IDataReader reader, string field, string defaultValue)
	{
		if (!(reader[field] is string result))
		{
			return defaultValue;
		}
		return result;
	}

	public static Guid GetGuid(this IDataReader reader, string field)
	{
		object obj = reader[field];
		if (obj is Guid)
		{
			return (Guid)obj;
		}
		return Guid.Empty;
	}

	public static Guid? GetNullableGuid(this IDataReader reader, string field)
	{
		object obj = reader[field];
		if (!(obj is Guid))
		{
			return null;
		}
		return (Guid?)obj;
	}

	public static DateTime GetDateTime(this IDataReader reader, string field)
	{
		return reader.GetDateTime(field, DateTime.MinValue);
	}

	public static DateTime GetDateTime(this IDataReader reader, string field, DateTime defaultValue)
	{
		object obj = reader[field];
		if (obj is DateTime)
		{
			return (DateTime)obj;
		}
		return defaultValue;
	}

	public static DateTime? GetNullableDateTime(this IDataReader reader, string field)
	{
		object obj = reader[field];
		if (!(obj is DateTime))
		{
			return null;
		}
		return (DateTime?)obj;
	}

	public static DateTimeOffset GetDateTimeOffset(this IDataReader reader, string field)
	{
		return new DateTimeOffset(reader.GetDateTime(field), TimeSpan.Zero);
	}

	public static DateTimeOffset GetDateTimeOffset(this IDataReader reader, string field, DateTimeOffset defaultValue)
	{
		DateTime dateTime = reader.GetDateTime(field);
		if (!(dateTime != DateTime.MinValue))
		{
			return defaultValue;
		}
		return new DateTimeOffset(dateTime, TimeSpan.Zero);
	}

	public static DateTimeOffset? GetNullableDateTimeOffset(this IDataReader reader, string field)
	{
		DateTime? nullableDateTime = reader.GetNullableDateTime(field);
		if (!nullableDateTime.HasValue)
		{
			return null;
		}
		return new DateTimeOffset(nullableDateTime.Value, TimeSpan.Zero);
	}

	public static int GetInt32(this IDataReader reader, string field)
	{
		return reader.GetInt32(field, 0);
	}

	public static int GetInt32(this IDataReader reader, string field, int defaultValue)
	{
		object obj = reader[field];
		if (obj is int)
		{
			return (int)obj;
		}
		return defaultValue;
	}

	public static int? GetNullableInt32(this IDataReader reader, string field)
	{
		object obj = reader[field];
		if (!(obj is int))
		{
			return null;
		}
		return (int?)obj;
	}

	public static long GetInt64(this IDataReader reader, string field)
	{
		return reader.GetInt64(field, 0);
	}

	public static long GetInt64(this IDataReader reader, string field, int defaultValue)
	{
		object obj = reader[field];
		if (obj is long)
		{
			return (long)obj;
		}
		return defaultValue;
	}

	public static long? GetNullableInt64(this IDataReader reader, string field)
	{
		object obj = reader[field];
		if (!(obj is long))
		{
			return null;
		}
		return (long?)obj;
	}

	public static decimal GetDecimal(this IDataReader reader, string field)
	{
		return reader.GetDecimal(field, 0L);
	}

	public static decimal GetDecimal(this IDataReader reader, string field, long defaultValue)
	{
		object obj = reader[field];
		if (obj is decimal)
		{
			return (decimal)obj;
		}
		return defaultValue;
	}

	public static decimal? GetNullableDecimal(this IDataReader reader, string field)
	{
		object obj = reader[field];
		if (!(obj is decimal))
		{
			return null;
		}
		return (decimal?)obj;
	}

	public static bool GetBoolean(this IDataReader reader, string field)
	{
		return reader.GetBoolean(field, defaultValue: false);
	}

	public static bool GetBoolean(this IDataReader reader, string field, bool defaultValue)
	{
		object obj = reader[field];
		if (obj is bool)
		{
			return (bool)obj;
		}
		return defaultValue;
	}

	public static bool? GetNullableBoolean(this IDataReader reader, string field)
	{
		object obj = reader[field];
		if (!(obj is bool))
		{
			return null;
		}
		return (bool?)obj;
	}

	public static Type GetType(this IDataReader reader, string field)
	{
		return reader.GetType(field, null);
	}

	public static Type GetType(this IDataReader reader, string field, Type defaultValue)
	{
		string @string = reader.GetString(field);
		if (@string.IsNullOrEmpty())
		{
			return defaultValue;
		}
		return Type.GetType(@string) ?? defaultValue;
	}

	public static object GetTypeInstance(this IDataReader reader, string field)
	{
		return reader.GetTypeInstance(field, null);
	}

	public static object GetTypeInstance(this IDataReader reader, string field, Type defaultValue)
	{
		Type type = reader.GetType(field, defaultValue);
		if (!(type != null))
		{
			return null;
		}
		return Activator.CreateInstance(type);
	}

	public static T GetTypeInstance<T>(this IDataReader reader, string field) where T : class
	{
		return reader.GetTypeInstance(field, null) as T;
	}

	public static T GetTypeInstanceSafe<T>(this IDataReader reader, string field, Type type) where T : class
	{
		return (reader.GetTypeInstance(field, null) as T) ?? (Activator.CreateInstance(type) as T);
	}

	public static T GetTypeInstanceSafe<T>(this IDataReader reader, string field) where T : class, new()
	{
		return (reader.GetTypeInstance(field, null) as T) ?? new T();
	}

	public static bool IsDBNull(this IDataReader reader, string field)
	{
		return reader[field] == DBNull.Value;
	}

	public static int ReadAll(this IDataReader reader, Action<IDataReader> action)
	{
		int num = 0;
		while (reader.Read())
		{
			action(reader);
			num++;
		}
		return num;
	}

	public static int IndexOf(this IDataRecord record, string name)
	{
		for (int i = 0; i < record.FieldCount; i++)
		{
			if (string.Compare(record.GetName(i), name, StringComparison.OrdinalIgnoreCase) == 0)
			{
				return i;
			}
		}
		return -1;
	}
}
